﻿<%@ Page Title="Users" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Users.aspx.cs" Inherits="Testify.Users" %>
<%@ MasterType VirtualPath="~/Site.Master" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <script type="text/javascript">
        $(document).ready(function () {

            $("#userBox1").show(400, function () {
                $("#userBox2").show(400);
                $("#MainContent_grdUsers").show(400);
            });
        });
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div class="container">
    <asp:Label ID="lblError" CssClass="failureNotification" runat="server" Text="Error" Visible="false" />
    <div id="users" class="centerElement">
        <div id="userBox1" class="userBox hidden" style="float:left;">  
            <h2 id="userBoxHeading1" class="userBoxHeading">Search for an Existing User</h2>
            <table>
                <tr>
                    <td align="right"><asp:Label ID="lblFilterUserType" runat="server" AssociatedControlID="ddlFilterUserType" Text="User Type:" Font-Italic="True" /></td>
                    <td>
                        <asp:DropDownList ID="ddlFilterUserType" runat="server" DataSourceID="dsUserType" DataTextField="user_type_title" DataValueField="user_type_id" AppendDataBoundItems="True">
                            <asp:ListItem />
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="dsUserType" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
                            SelectCommand="SELECT user_type_id, user_type_title
  FROM singularity.user_type
 WHERE user_type_id &gt; 0
 ORDER BY user_type_title" />
                    </td>
                </tr>
                <tr>
                    <td align="right"><asp:Label ID="lblFilterUserName" runat="server" Text="User Name:" AssociatedControlID="txtFilterUserName" Font-Italic="True" /></td>
                    <td><asp:TextBox ID="txtFilterUserName" runat="server" /></td>
                </tr>
                <tr>
                    <td align="right"><asp:Label ID="lblFilterFirstName" runat="server" Text="First Name:" AssociatedControlID="txtFilterFirstName" Font-Italic="True" /></td>
                    <td><asp:TextBox ID="txtFilterFirstName" runat="server" /></td>
                </tr>
                <tr>
                    <td align="right"><asp:Label ID="lblFilterLastName" runat="server" Text="Last Name:" AssociatedControlID="txtFilterLastName" Font-Italic="True" /></td>
                    <td><asp:TextBox ID="txtFilterLastName" runat="server" /></td>
                </tr>
                <tr>
                    <td align="right" colspan="2"><asp:Button ID="btnFind" CssClass="btn_details" runat="server" Text="Find" /></td>
                </tr>
            </table>
            <span style="width:100px;margin-right:20px;">&nbsp;</span><asp:CheckBox ID="chkShowDeleted" runat="server" Text="Show Deleted Users" AutoPostBack="true" />
        </div>

        <div id="userBox2" class="userBox">  
            <h2 id="userBoxHeading2" class="userBoxHeading">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add a New User</h2>
            <table>
                <tr>
                    <td align="right"><asp:Label ID="lblUserType" runat="server" AssociatedControlID="ddlUserType" Text="User Type:" Font-Bold="True" /></td>
                    <td>
                        <asp:DropDownList ID="ddlUserType" runat="server" DataSourceID="dsUserType" DataTextField="user_type_title" DataValueField="user_type_id" AppendDataBoundItems="True" >
                            <asp:ListItem />
                        </asp:DropDownList>
                    </td>
                    <td><asp:RequiredFieldValidator ID="valUserType" runat="server" ControlToValidate="ddlUserType" Display="Dynamic" ErrorMessage="Required" ForeColor="Red" SetFocusOnError="True" ValidationGroup="grpAdd" /></td>
                </tr>
                <tr>
                    <td align="right"><asp:Label ID="lblUserName" runat="server" AssociatedControlID="txtUserName" Text="User Name:" Font-Bold="True" /></td>
                    <td><asp:TextBox ID="txtUserName" runat="server" /></td>
                    <td><asp:RequiredFieldValidator ID="valUserName" runat="server" ControlToValidate="txtUserName" Display="Dynamic" ErrorMessage="Required" ForeColor="Red" SetFocusOnError="True" ValidationGroup="grpAdd" /></td>
                </tr>
                <tr>
                    <td align="right"><asp:Label ID="lblPassWord" runat="server" AssociatedControlID="txtPassWord" Text="Pass Word:" Font-Bold="True" /></td>
                    <td><asp:TextBox ID="txtPassWord" runat="server" TextMode="Password" /></td>
                    <td><asp:RequiredFieldValidator ID="valPassWord" runat="server" ControlToValidate="txtPassWord" Display="Dynamic" ErrorMessage="Required" ForeColor="Red" SetFocusOnError="True" ValidationGroup="grpAdd" /></td>
                </tr>
                <tr>
                    <td align="right"><asp:Label ID="lblFirstName" runat="server" AssociatedControlID="txtFirstName" Text="First Name:" Font-Bold="True" /></td>
                    <td><asp:TextBox ID="txtFirstName" runat="server" /></td>
                    <td><asp:RequiredFieldValidator ID="valFirstName" runat="server" ControlToValidate="txtFirstName" Display="Dynamic" ErrorMessage="Required" ForeColor="Red" SetFocusOnError="True" ValidationGroup="grpAdd" /></td>
                </tr>
                <tr>
                    <td align="right"><asp:Label ID="lblLastName" runat="server" AssociatedControlID="txtLastName" Text="Last Name:" Font-Bold="True" /></td>
                    <td><asp:TextBox ID="txtLastName" runat="server" /></td>
                    <td><asp:RequiredFieldValidator ID="valLastName" runat="server" ControlToValidate="txtLastName" Display="Dynamic" ErrorMessage="Required" ForeColor="Red" SetFocusOnError="True" ValidationGroup="grpAdd" /></td>
                </tr>
                <tr>
                    <td align="right" colspan="2"><asp:Button ID="btnInsert" CssClass="btn_details" runat="server" Text="Add" onclick="btnInsert_Click" ValidationGroup="grpAdd" /></td>
                    <td>&nbsp;</td>
                </tr>
            </table>
        </div>
    </div>

    <asp:GridView ID="grdUsers" runat="server" CssClass="hidden" DataSourceID="dsUsers" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" AllowPaging="True" 
        AllowSorting="True" DataKeyNames="user_id" AutoGenerateColumns="False" EmptyDataText="There are no standard users that meet the search critera." onrowdatabound="grdUsers_RowDataBound" onrowupdated="grdUsers_RowUpdated">
        <Columns>
            <asp:TemplateField HeaderText="User ID" SortExpression="user_id">
                <EditItemTemplate>
                    <asp:Label ID="lblGrdUserIDRO" runat="server" Text='<%# Bind("user_id") %>' />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblGrdUserID" runat="server" Text='<%# Bind("user_id") %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="User Type" SortExpression="user_type_id">
                <EditItemTemplate>
                    <asp:Label ID="lblGrdUserTypeRO" runat="server" Text='<%# Eval("user_type_title") %>' />
                    <asp:HiddenField ID="hdnGrdUserTypeIDRO" runat="server" Value='<%# Bind("user_type_id") %>' />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblGrdUserType" runat="server" Text='<%# Eval("user_type_title") %>' />
                    <asp:HiddenField ID="hdnGrdUserTypeID" runat="server" Value='<%# Bind("user_type_id") %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="User Name" SortExpression="user_name">
                <EditItemTemplate>
                    <asp:TextBox ID="txtGrdUserName" runat="server" Text='<%# Bind("user_name") %>' />
                    <asp:RequiredFieldValidator ID="valGrdUserName" runat="server" ControlToValidate="txtGrdUserName" Display="Dynamic" ErrorMessage="Required" ForeColor="Red" SetFocusOnError="True" />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblGrdUserName" runat="server" Text='<%# Bind("user_name") %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Pass Word" SortExpression="user_password">
                <EditItemTemplate>
                    <asp:TextBox ID="txtGrdPassWord" runat="server" TextMode="Password" Text='<%# Bind("user_password") %>' />
                    <asp:RequiredFieldValidator ID="valGrdPassWord" runat="server" ControlToValidate="txtGrdPassWord" Display="Dynamic" ErrorMessage="Required" ForeColor="Red" SetFocusOnError="True" />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblGrdPassWord" runat="server" Text='<%# Bind("user_password") %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="First Name" SortExpression="user_first_name">
                <EditItemTemplate>
                    <asp:TextBox ID="txtGrdFirstName" runat="server" Text='<%# Bind("user_first_name") %>' />
                    <asp:RequiredFieldValidator ID="valGrdFirstName" runat="server" ControlToValidate="txtGrdFirstName" Display="Dynamic" ErrorMessage="Required" ForeColor="Red" SetFocusOnError="True" />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblGrdFirstName" runat="server" Text='<%# Bind("user_first_name") %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Last Name" SortExpression="user_last_name">
                <EditItemTemplate>
                    <asp:TextBox ID="txtGrdLastName" runat="server" Text='<%# Bind("user_last_name") %>' />
                    <asp:RequiredFieldValidator ID="valGrdLastName" runat="server" ControlToValidate="txtGrdLastName" Display="Dynamic" ErrorMessage="Required" ForeColor="Red" SetFocusOnError="True" />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblGrdLastName" runat="server" Text='<%# Bind("user_last_name") %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="user_maint" HeaderText="Maint" SortExpression="user_maint" ReadOnly="True" Visible="false" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="dsUsers" runat="server" CancelSelectOnNullParameter="False" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
        SelectCommand="
SELECT user_id, user_type_id, user_type_title, user_name, user_password, user_first_name, user_last_name, user_maint
  FROM singularity.testify_user
       JOIN singularity.user_type USING (user_type_id)
 WHERE (:p_UserTypeID    IS NULL OR user_type_id    = :p_UserTypeID)
   AND (:p_UserName      IS NULL OR UPPER(TRIM(user_name))       LIKE '%' || UPPER(TRIM(:p_UserName))      || '%')
   AND (:p_UserFirstName IS NULL OR UPPER(TRIM(user_first_name)) LIKE '%' || UPPER(TRIM(:p_UserFirstName)) || '%')
   AND (:p_UserLastName  IS NULL OR UPPER(TRIM(user_last_name))  LIKE '%' || UPPER(TRIM(:p_UserLastName))  || '%')
   AND (:p_ShowDeleted   =  1    OR user_maint     != 'D')
   AND user_type_id &gt; 0
 ORDER BY user_id" DeleteCommand="
BEGIN
  singularity.users.deleteUser(p_AdminID =&gt; :admin_id, p_UserID =&gt; :user_id);
END;" UpdateCommand="
BEGIN
  singularity.users.changeUser(
    p_AdminID       =&gt; :admin_id,
    p_UserID        =&gt; :user_id,
    p_UserTypeID    =&gt; :user_type_id,
    p_UserName      =&gt; :user_name,
    p_UserPassword  =&gt; :user_password,
    p_UserFirstName =&gt; :user_first_name,
    p_UserLastName  =&gt; :user_last_name);
END;" InsertCommand="
BEGIN
  :p_UserID := singularity.users.addUser(
    p_AdminID       =&gt; :admin_id,
    p_UserTypeID    =&gt; :p_UserTypeID,
    p_UserName      =&gt; :p_UserName,
    p_UserPassword  =&gt; :p_UserPassword,
    p_UserFirstName =&gt; :p_UserFirstName,
    p_UserLastName  =&gt; :p_UserLastName);
END;">
        <DeleteParameters>
            <asp:SessionParameter Name="admin_id" SessionField="UserID" />
            <asp:ControlParameter ControlID="grdUsers" Name="user_id" PropertyName="SelectedValue" />
        </DeleteParameters>
        <InsertParameters>
            <asp:SessionParameter Name="admin_id" SessionField="UserID" />
            <asp:Parameter Direction="ReturnValue" Name="p_UserID" Size="40" />
            <asp:ControlParameter ControlID="ddlUserType" Name="p_UserTypeID" PropertyName="SelectedValue" />
            <asp:ControlParameter ControlID="txtUserName" Name="p_UserName" PropertyName="Text" />
            <asp:ControlParameter ControlID="txtPassWord" Name="p_UserPassword" PropertyName="Text" />
            <asp:ControlParameter ControlID="txtFirstName" Name="p_UserFirstName" PropertyName="Text" />
            <asp:ControlParameter ControlID="txtLastName" Name="p_UserLastName" PropertyName="Text" />
        </InsertParameters>
        <SelectParameters>
            <asp:ControlParameter ControlID="ddlFilterUserType" Name="p_UserTypeID" PropertyName="SelectedValue" />
            <asp:ControlParameter ControlID="txtFilterUserName" Name="p_UserName" PropertyName="Text" />
            <asp:ControlParameter ControlID="txtFilterFirstName" Name="p_UserFirstName" PropertyName="Text" />
            <asp:ControlParameter ControlID="txtFilterLastName" Name="p_UserLastName" PropertyName="Text" />
            <asp:ControlParameter ControlID="chkShowDeleted" Name="p_ShowDeleted" PropertyName="Checked" />
        </SelectParameters>
        <UpdateParameters>
            <asp:SessionParameter Name="admin_id" SessionField="UserID" />
            <asp:ControlParameter ControlID="grdUsers" Name="user_id" PropertyName="SelectedValue" />
            <asp:ControlParameter ControlID="grdUsers" Name="user_type_id" PropertyName="SelectedValue" />
            <asp:ControlParameter ControlID="grdUsers" Name="user_name" PropertyName="SelectedValue" />
            <asp:ControlParameter ControlID="grdUsers" Name="user_password" PropertyName="SelectedValue" />
            <asp:ControlParameter ControlID="grdUsers" Name="user_first_name" PropertyName="SelectedValue" />
            <asp:ControlParameter ControlID="grdUsers" Name="user_last_name" PropertyName="SelectedValue" />
        </UpdateParameters>
    </asp:SqlDataSource>
</div>
</asp:Content>
